package com.gree.flowable.app.controller;

import com.gree.flowable.app.mapper.FormInterfaceInfoMapper;
import com.gree.flowable.app.pojo.form.IFormDataVO;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFormDataService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/4/6 9:33
 * @description：表单数据API
 * @modified By：
 * @version: 1.0
 */
@RestController

@Api(tags = "表单数据API")
@RequestMapping("/rest/formData")
public class IFormDataApi extends FlowableBaseApi {
    @Autowired
    private IFormDataService iFormDataService;

    @Autowired
    private FormInterfaceInfoMapper formInterfaceInfoMapper;

//    @Autowired
//    private MyClient myClient;

    /**
     * 添加表单数据
     * @param params {
     * 	"businessKey":"test001",
     * 	"formId":"EDG",
     * 	"data":{
     * 		"name":"张三",
     * 		"num":18
     *        }
     * }
     * @return
     */
    @PostMapping("/addFormData")
    @ApiOperation("添加表单数据")
    public ReturnVO addFormData(@RequestBody Map<String,Object> params){
        ReturnVO result;
        String businessKey = params.get("businessKey") == null ? "" : params.get("businessKey").toString();
        String formId = params.get("formId") == null ? "" : params.get("formId").toString();
        Map<String,Object> data = (Map<String,Object>) params.get("data");
        result  = iFormDataService.addFormData(businessKey, formId, data);
        return result;
    }

    @PostMapping("/updateFormData")
    @ApiOperation("修改表单数据")
    public ReturnVO updateFormData(@RequestBody IFormDataVO iFormDataVO){
        ReturnVO result;
//        String businessKey = params.get("businessKey") == null ? "" : params.get("businessKey").toString();
//        String formId = params.get("formId") == null ? "" : params.get("formId").toString();
//        Map data = (Map) params.get("data");
        String businessKey = iFormDataVO.getBusinessKey();
        String formId = iFormDataVO.getFormId();
        Map<String, Object> data = iFormDataVO.getData();
        result = iFormDataService.updateFormData(businessKey, formId, data);
        return result;
    }

    /**
     * 删除表单数据
     * @param businessKey{
     * 	"businessKey":"test001"
     * }
     * @return
     */
    @GetMapping("/deleteFormData/{formId}/{businessKey}")
    @ApiOperation("删除表单数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessKey",value = "流程实例id",dataType = "String"),
            @ApiImplicitParam(name = "formId",value = "表单id(表名)",dataType = "String")
    })

    public ReturnVO deleteFormData(@PathVariable String businessKey, @PathVariable String formId){
        ReturnVO returnVO;
        Integer deleteTotal = iFormDataService.deleteFormData(businessKey, formId);
        if (deleteTotal > 0){
            return returnVO = new ReturnVO(ReturnCode.SUCCESS,"数据删除成功");
        } else {
            return returnVO = new ReturnVO(ReturnCode.SUCCESS,"数据删除失败");
        }
    }

    /**
     * 查询表单数据
     * @param taskId 流程实例id(主键)
     * @return
     */
    @PostMapping("/queryFormDataByBusinessKey/{taskId}")
    @ApiOperation("查询表单数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId",value = "任务ID",dataType = "String")
    })
    public ReturnVO queryFormDataByBusinessKey(@PathVariable String taskId){
        ReturnVO returnVO ;
        Map<String, Object> dataMap = iFormDataService.selectFormDataByBusinessKey(taskId);
        Map<String,Object> resultMap = new HashMap<>();
        for (String key : dataMap.keySet()) {
            resultMap.put(key.toLowerCase(),dataMap.get(key));
        }
        if (resultMap.size() > 0){
            return returnVO = new ReturnVO(ReturnCode.SUCCESS,"数据查询成功",resultMap);
        } else {
            return returnVO = new ReturnVO(ReturnCode.SUCCESS,"数据查询失败",resultMap);
        }
    }


//    @PostMapping("/callBack")
//    public ReturnVO addFormDataCallBack(@RequestBody IFormDataVO iFormDataVO){
//        ReturnVO result = null;
//        String clientId = SecurityUtils.getClientId();
//        String systemName = null;
//        switch (clientId){
//            case "gscm_client":
//                systemName = null;
//                break;
//            case "external_client":
//                systemName = SecurityUtils.getUser().getUsername();
//                break;
//            default:
//        }
//        String businessKey = iFormDataVO.getBusinessKey();
//        String formId = iFormDataVO.getFormId();
//        Map<String, Object> data = iFormDataVO.getData();
//        ReturnVO addFormData = iFormDataService.addFormData(businessKey, formId, data);
//        if (ReturnCode.SUCCESS.equals(addFormData.getCode())){
//            QueryWrapper<FormInterfaceInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(FormInterfaceInfo::getUserName,systemName).eq(FormInterfaceInfo::getInterfaceType,"0");
//            FormInterfaceInfo formInterfaceInfo = formInterfaceInfoMapper.selectOne(queryWrapper);
//            String url = formInterfaceInfo.getInterfaceAddress();
//            myClient.postCallBack(url,data);
//            result = new ReturnVO(ReturnCode.SUCCESS,"数据保存成功，并成功回调");
//        } else {
//            result = new ReturnVO(ReturnCode.FAIL,"数据保存失败，请检查数据");
//        }
//        return result;
//    }

}
